/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-1-17 下午5:18 Created by dwzhang
 */
package com.jade.modules.cms.impl;

import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;
import com.jade.modules.cms.CategoryDao;
import com.jade.modules.cms.CmsCategory;
import com.jade.modules.cms.CmsException;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;

/**
 * 采用方式实现的数据访问接口。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-1-17 下午5:18
 */
//@Named ("module_cmsCategoryDao")
public class JpaCategoryDao implements CategoryDao
{
    @PersistenceContext (unitName = "default")
    protected EntityManager entityManager;

    @Caching (evict = {@CacheEvict (value = "module.cms.category.g", key = "#category.id"),
            @CacheEvict (value = "module.cms.category.l",
                    key = "#category.ownerType + '_' + #category.ownerId + '[%GRP%]'"),
            @CacheEvict (value = "module.cms.category.qa", allEntries = true),
            @CacheEvict (value = "module.cms.category.q",
                    key = "#category.ownerType + '_' + #category.ownerId + '[%GRP%]'")})
    public void add(CmsCategory category)
            throws CmsException
    {
        entityManager.persist(category);
    }

    @Caching (evict = {@CacheEvict (value = "module.cms.category.g", key = "#category.id"),
            @CacheEvict (value = "module.cms.category.l",
                    key = "#category.ownerType + '_' + #category.ownerId + '[%GRP%]'"),
            @CacheEvict (value = "module.cms.category.qa", allEntries = true),
            @CacheEvict (value = "module.cms.category.q",
                    key = "#category.ownerType + '_' + #category.ownerId + '[%GRP%]'")})
    public void update(CmsCategory category)
            throws CmsException
    {
        entityManager.merge(category);
    }

    @Caching (evict = {@CacheEvict (value = "module.cms.category.g", key = "#category.id"),
            @CacheEvict (value = "module.cms.category.l",
                    key = "#category.ownerType + '_' + #category.ownerId + '[%GRP%]'"),
            @CacheEvict (value = "module.cms.category.qa", allEntries = true),
            @CacheEvict (value = "module.cms.category.q",
                    key = "#category.ownerType + '_' + #category.ownerId + '[%GRP%]'")})
    public void remove(CmsCategory category)
            throws CmsException
    {
        CmsCategory old = entityManager.find(CmsCategory.class, category.getId());
        if (old != null) {
            entityManager.remove(old);
        }
    }

    @Cacheable (value = "module.cms.category.g", key = "#id")
    public CmsCategory get(long id)
    {
        return entityManager.find(CmsCategory.class, id);
    }

    @Cacheable (value = "module.cms.category.l", key = "#ownerType + '_' + #ownerId + '[%GRP%]' + #criteria")
    public List<CmsCategory> list(String ownerType, String ownerId, Criteria criteria)
    {
        criteria.add(Restrictions.eq("ownerType", ownerType));
        criteria.add(Restrictions.eq("ownerId", ownerId));
        return SqlUtils.sqlQueryWithoutCount(SQL, entityManager, criteria);
    }

    @Cacheable (value = "module.cms.category.q", key = "#ownerType + '_' + #ownerId + '[%GRP%]' + #criteria")
    public PagingList<CmsCategory> query(String ownerType, String ownerId, Criteria criteria)
    {
        criteria.add(Restrictions.eq("ownerType", ownerType));
        criteria.add(Restrictions.eq("ownerId", ownerId));
        return SqlUtils.sqlQuery(SQL, entityManager, criteria);
    }

    @Cacheable (value = "module.cms.category.qa", key = "#criteria")
    public PagingList<CmsCategory> query(Criteria criteria)
    {
        return SqlUtils.sqlQuery(SQL, entityManager, criteria);
    }

    protected static final String SQL = "SELECT category FROM com.jade.modules.cms.CmsCategory category";
}